from dataclasses import dataclass
from jinja2 import Template, FileSystemLoader, Environment

@dataclass
class ConfigTemplate:
    owner: str = 'airflow'
    depends_on_past: bool = False
    start_date: str = "datetime(2024, 11, 27)"
    email_on_failure: bool = False
    email_on_retry: bool = False
    retries: int = 0

@dataclass
class DAGTemplate:
    dag_id: str = "user-submit-dag"
    default_args: str = "default_args"
    description: str = 'a user submit dag'
    template_searchpath: str = "['/opt/airflow']"
    schedule_interval:str = "None"

j2_loader = FileSystemLoader('./j2_files')
env = Environment(loader=j2_loader)

def create_dag_args(config : ConfigTemplate):
    j2_tmpl = env.get_template('./dag_args.j2')
    result = j2_tmpl.render(
        owner=config.owner,
        depends_on_past=config.depends_on_past,
        start_date=config.start_date,
        email_on_failure=config.email_on_failure,
        email_on_retry=config.email_on_retry,
        retries=config.retries,
    )
    return result

def create_dag(dag : DAGTemplate):
    j2_tmpl = env.get_template('./dag.j2')
    result = j2_tmpl.render(
        dag_id=dag.dag_id,
        default_args=dag.default_args,
        description=dag.description,
        template_searchpath=dag.template_searchpath,
        schedule_interval=dag.schedule_interval,
    )
    return result